package algorithm.dailyQuestion.day20250319;

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Solution solution = new Solution();
        List<List<Integer>> matrix = solution.findMatrix(new int[]{1, 3, 4, 1, 2, 3, 1});
        System.out.println(matrix);
    }

    static  class Solution {
        public List<List<Integer>> findMatrix(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
            Map<Integer,Integer> map = new HashMap<>();
            int maxCount = 0;
            for(Integer num : nums) {
                int count = map.getOrDefault(num, 0) + 1;
                maxCount = Math.max(maxCount, count);
                map.put(num, count);
            }
            for(int i = 0; i < maxCount; i++) {
                res.add(new ArrayList<>());
                Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
                for(Map.Entry<Integer, Integer> entry : entries) {
                    if(entry.getValue() > 0) {
                        res.get(i).add(entry.getKey());
                        entry.setValue(entry.getValue() - 1);
                    }
                }
            }

            return res;

        }
    }
}
